package xyz.tanxiao.links.common.enums;

import lombok.Getter;
import xyz.tanxiao.links.common.exception.GeneralAccessException;
import xyz.tanxiao.links.common.exception.PasswordAccessException;
import xyz.tanxiao.links.common.exception.ToShortLinkException;

/**
 * 限流目标接口枚举
 *
 * @author 万成波
 * @email 2300064869@qq.com
 * @createTime 2023-06-23 05:02:58 星期五
 */
@Getter
public enum LimitTargetEnum {

	/**
	 * 长链转换短链接口限流
	 */
	TO_SHORT_LINK {
		@Override
		public String getLimitCacheKey(int limitLevel, String ip) {
			return LinksCacheKeyEnum.TO_LINK_LIMIT_CACHE_KEY.getCacheKey() + limitLevel + "_" + ip;
		}
		@Override
		public RuntimeException getLimitException() {
			return new ToShortLinkException(LinksCacheKeyEnum.TO_LINK_LIMIT_CACHE_KEY.getMessage());
		}
		@Override
		public RuntimeException getLimitException(String message) {
			return new ToShortLinkException(message);
		}
	},

	/**
	 * 短链常规访问接口限流
	 */
	GENERAL_ACCESS {
		@Override
		public String getLimitCacheKey(int limitLevel, String ip) {
			return LinksCacheKeyEnum.GENERAL_ACCESS_LIMIT_CACHE_KEY.getCacheKey() + limitLevel + "_" + ip;
		}
		@Override
		public RuntimeException getLimitException() {
			return new GeneralAccessException(LinksCacheKeyEnum.GENERAL_ACCESS_LIMIT_CACHE_KEY.getMessage());
		}
		@Override
		public RuntimeException getLimitException(String message) {
			return new GeneralAccessException(message);
		}
	},

	/**
	 * 短链密码访问接口限流
	 */
	PASSWORD_ACCESS {
		@Override
		public String getLimitCacheKey(int limitLevel, String ip) {
			return LinksCacheKeyEnum.PASSWORD_ACCESS_LIMIT_CACHE_KEY.getCacheKey() + limitLevel + "_" + ip;
		}
		@Override
		public RuntimeException getLimitException() {
			return new PasswordAccessException(LinksCacheKeyEnum.PASSWORD_ACCESS_LIMIT_CACHE_KEY.getMessage());
		}
		@Override
		public RuntimeException getLimitException(String message) {
			return new PasswordAccessException(message);
		}
	},


	/**
	 * 批量长链转换短链接口限流
	 */
	BATCH_TO_SHORT_LINK {
		@Override
		public String getLimitCacheKey(int limitLevel, String ip) {
			return LinksCacheKeyEnum.BATCH_TO_LINK_LIMIT_CACHE_KEY.getCacheKey() + limitLevel + "_" + ip;
		}
		@Override
		public RuntimeException getLimitException() {
			return new ToShortLinkException(LinksCacheKeyEnum.BATCH_TO_LINK_LIMIT_CACHE_KEY.getMessage());
		}
		@Override
		public RuntimeException getLimitException(String message) {
			return new ToShortLinkException(message);
		}
	};


	/**
	 * 获取限流缓存Key
	 */
	public abstract String getLimitCacheKey(int limitLevel, String accessIp);

	/**
	 * 获取限流业务异常
	 */
	public abstract RuntimeException getLimitException();

	/**
	 * 获取限流业务异常
	 */
	public abstract RuntimeException getLimitException(String message);

}